import { track, trigger } from "./effect";

function createGetter(isReadonly: boolean = false) {
    return function get<T extends object>(target: T, key: string, receiver: any) {
        const value = Reflect.get(target, key, receiver);
        //TODO 收集依赖
        track(target, key);
        return value;
    }
}

function createSetter() {
    return function set<T extends object>(target: T, key: string, value: any, receiver: any): boolean {
        const result = Reflect.set(target, key, value);
        //TODO 触发依赖
        trigger(target, key);
        return result
    }
}


export const mutableHandlers = {
    get: createGetter(),
    set: createSetter()
}
export const readonlyHandlers = {
    get: createGetter(true),
    set(target: any, key: string, value: any, receiver: any) {
        return true
    }
}
